package com.woniuxy.credit_loans.collection_zhang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.collection_zhang.dto.PageDto;
import com.woniuxy.credit_loans.collection_zhang.service.CollectionService;
import com.woniuxy.credit_loans.entity.Borwarning;
import com.woniuxy.credit_loans.entity.Collection;
import com.woniuxy.credit_loans.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Collection")
public class CollectionController {


    @Autowired
    private CollectionService collectionService;

    @PostMapping("/page")
    public Result selectAll(@RequestBody PageDto pageDto){
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getPagesize(),true);
         List<Collection> list = collectionService.selectAll(pageDto.getInput());
        PageInfo<Collection> pageInfo = new PageInfo<Collection>(list);

        return  Result.ok().data("list",list).data("pageInfo",pageInfo.getTotal());
    }

    @PostMapping("")
    public  Result add(@RequestBody Collection collection){
         int i = collectionService.insertSelective(collection);
        if (i==0) {
            return Result.ok();
        }else {
            return  Result.error();
        }
    }

    @GetMapping("{id}")
    public  Result selectById(@PathVariable Long id){
         Collection collection = collectionService.selectByPrimaryKey(id);
         return  Result.ok().data("collection",collection);
    }

    @PutMapping("")
    public  Result update(@RequestBody Collection collection){
        int i = collectionService.updateByPrimaryKeySelective(collection);
        if (i==0) {
            return Result.ok();
        }else {
            return  Result.error();
        }
    }

    @DeleteMapping("{id}")
    public  Result Delete(@PathVariable Long id){
        int i=collectionService.deleteByPrimaryKey(id);
        if (i==0) {
            return Result.ok();
        }else {
            return  Result.error();
        }
    }
}
